package com.example.baselibrary.Base;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.appcompat.app.AppCompatActivity;

import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.LogUtil;
import com.example.baselibrary.Utils.SPUtils;
import com.example.baselibrary.Utils.StringUtil;
import com.example.baselibrary.wigitview.LosLoadDialog;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public abstract class BaseActivity extends AppCompatActivity {

   static ArrayList<Activity> activities;
    public String Base_URL;
    public Map<String, String> headers;
    public LosLoadDialog iosLoadDialog;

    Intent systemBarIntent = new Intent("com.tchip.changeBarHideStatus");

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        makeStatusBarTransparent(this);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getView());

        if (activities == null) {
            activities = new ArrayList<>();
        }
        addActivity();

        String url = (String) SPUtils.get(this, Customer.LiSHI_INPUT_URL, "");
        if (StringUtil.isNullOrEmpty(url)) {
            Base_URL = Customer.LiSHI_BASE_URL;
        }else {
            Base_URL = url;
        }


        //获取headmap
        if (headers == null) {
            headers = new HashMap<>();
        }
        String token = (String) SPUtils.get(this, Customer.LISHI_ACCESSTOKEN, "");
        if (!StringUtil.isNullOrEmpty(token)) {
            headers.put("Token", token);
        }

        if (iosLoadDialog == null) {
            //初始化菊花
            iosLoadDialog = new LosLoadDialog(this);

        }
        init();
        hintBottom();
        hideInput();
    }

    private void hintBottom() {
//        Window _window = getWindow();
//                WindowManager.LayoutParams params = _window.getAttributes();
//        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE|View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_LOW_PROFILE;
//        _window.setAttributes(params);
        execRootCmdSilent("settings put system systembar_hide 1");//1为隐藏导航栏，0显示导航栏
        this.sendBroadcast(systemBarIntent);

    }

    protected abstract int getView();

    public abstract void init();

    @Override
    protected void onResume() {
        super.onResume();
        execRootCmdSilent("settings put system systembar_hide 1");//1为隐藏导航栏，0显示导航栏
        this.sendBroadcast(systemBarIntent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    public void addActivity() {
        activities.add(this);
        for (int i = 0; i < activities.size(); i++) {
            LogUtil.d("添加123：" + activities.get(i).getLocalClassName());
        }

    }


    public void removeActivity() {
        activities.remove(this);

    }

    /**
     * 结束所以活动
     */
    public void finishAllAcitivity() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
        execRootCmdSilent("settings put system systembar_hide 0");//1为隐藏导航栏，0显示导航栏
        this.sendBroadcast(systemBarIntent);
    }

    private static int execRootCmdSilent(String cmd) {
        int result = -1;
        DataOutputStream dos = null;

        try {
            Process p = Runtime.getRuntime().exec("su");
            dos = new DataOutputStream(p.getOutputStream());
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            p.waitFor();
            result = p.exitValue();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
//                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static void makeStatusBarTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            window.getDecorView().setSystemUiVisibility(option);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 隐藏键盘
     */
    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

}
